#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import os
import re

import logging
logging.getLogger().setLevel(logging.INFO)
#logging.error("++++++++++++ 1 cwd = %s, path = %s" % (os.getcwd(), os.environ["Path"] ))

ProjectPath = os.path.dirname(os.getcwd())
sys.path.insert(0, ProjectPath)
sys.path.insert(0, os.path.join(ProjectPath, 'common'))
sys.path.insert(0, os.path.join(ProjectPath, 'database'))

#logging.error("++++++++++++ 2 cwd = %s, path = %s" % (os.getcwd(), os.environ["Path"] ))

from sgmllib import SGMLParser
import wsgiref.handlers

from google.appengine.api import urlfetch
from django.utils import simplejson
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template


import databasefunction
import commonfunction
import rss.rssdatabase as rssdatabase



class CTaskResetSelfRssPage(commonfunction.CBasePage):
    def get(self):
#        logging.error("----------CTaskResetSelfRssPage 0 ")
#        NickName = simplejson.loads(NickName)
#        logging.error("----------CCheckUserRPCPage 2 NickName = %s" % (NickName))

        rssdatabase.ResetRssCatch('hotblog')
        rssdatabase.ResetRssCatch('newblog')
        rssdatabase.ResetRssCatch('hotcollect')
        rssdatabase.ResetRssCatch('newcollect')
        rssdatabase.ResetRssCatch('hotlike')
        rssdatabase.ResetRssCatch('newlike')
        
        self.response.out.write("OK")



class TianYaSGMLParser(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)

        self.CurHref = ''
        self.FoundTopNews = False
        self.FoundTopNewsUL = False
        self.TopNews = {}               #头条新闻

        self.FoundHotspot = False       #发现：热点推荐
        self.HaveHotspot = False        #已经添加完热点新闻
        self.HotspotNews = {}           #热点新闻

        self.CanRecord = False
        
        
    def start_div(self, attrs):
        DivClassName = [v for k, v in attrs if k == 'class']
        if 0 < len(DivClassName):
            if 'topnews' == DivClassName[0]:
#                logging.error( "----------start_div 2 DivClassName[0] = %s" % (DivClassName[0]) )
                self.FoundTopNews = True
	
    def start_ul(self, attrs):
        if self.FoundTopNews:
            self.FoundTopNewsUL = True
            self.CanRecord = True
        if self.FoundHotspot:
            self.CanRecord = True
            
            
    def end_ul(self):
        if self.FoundTopNewsUL:
            self.FoundTopNewsUL = False
            self.FoundTopNews = False
        elif self.FoundHotspot:
            self.HaveHotspot = True
            self.FoundHotspot = False
            
        self.CanRecord = False
        self.CurHref = ''
        
            
    def start_h1(self, attrs):
        if self.FoundTopNews:
            self.CanRecord = True
            
    def end_h1(self):
        if self.FoundTopNews:
            self.CanRecord = False


    def start_a(self, attrs):
        if self.CanRecord:
#            for attr in attrs:
#                logging.error("----------start_a 1 attr = %s" % (";".join(attr)))
                
            href = [v for k, v in attrs if k == 'href']  
            if href:
#                logging.error("----------start_a 2 href = %s" % (href[0]))
                self.CurHref = href[0]
                

    def handle_data(self, text):
        UnicodeText = unicode(text, 'utf8')
        UnicodeText.strip(u' ')
        if 1 > len(UnicodeText):
            return
      
        if self.CanRecord and 0 < len(self.CurHref):
            if self.FoundHotspot:
                if False == self.HotspotNews.has_key(self.CurHref):
                    self.HotspotNews[self.CurHref] = UnicodeText
#                    logging.error("+++++++ handle_data 2 Curhref = %s, Data = %s" % (self.CurHref, UnicodeText.encode('gb2312')))
            else:
                if False == self.TopNews.has_key(self.CurHref):
                    self.TopNews[self.CurHref] = UnicodeText
#                    logging.error("--*********---handle_data 3 Curhref = %s, Data = %s" % (self.CurHref, UnicodeText.encode('gb2312')))
                    
#            logging.error(u"--*********----handle_data 2 Curhref = %s, Data = %s" % (self.CurHref, UnicodeText))
        elif False == self.FoundHotspot:
            if u'热点推荐' == UnicodeText:
                self.FoundHotspot = True




class CTaskGetHotPage(commonfunction.CBasePage):
    def get(self):
        Language = self.GetTheSessionLanguage()
        TimeDelta = self.GetTheSessionTimeDelta()
        
        Target = self.request.get('target').strip().lower()
#        logging.error("----------CCheckUserRPCPage 1 NickName = %s" % (NickName))
#        NickName = simplejson.loads(NickName)
#        logging.error("----------CCheckUserRPCPage 2 NickName = %s" % (NickName))
        if 'tianya' == Target:
            Result = urlfetch.fetch('http://www.tianya.cn/focus/index.shtml')
            if Result.status_code == 200:
                Parser = TianYaSGMLParser()
                Parser.feed(Result.content)

                rssdatabase.SetTianYaTopRss(Parser.TopNews, Language, TimeDelta)
                rssdatabase.SetTianYaHotRss(Parser.HotspotNews, Language, TimeDelta)
                
                self.response.out.write(u"头条<br>")
                for k, v in Parser.TopNews.items():
                    self.response.out.write(u"<a href='%s'>%s</a><br>" % (k, v))

                self.response.out.write(u"热点<br>")
                for k, v in Parser.HotspotNews.items():
                    self.response.out.write(u"<a href='%s'>%s</a><br>" % (k, v))


        elif 'maopu' == Target:
            Result = urlfetch.fetch('http://www.mop.com')
                

        
#        self.response.out.write("OK")


def main():
    application = webapp.WSGIApplication([
                                    ('/task_reset_self_rss', CTaskResetSelfRssPage),
                                    ('/task_get_hot', CTaskGetHotPage),
                                    ],
                                    debug=True)
    
    wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    main()


